package com.shopping.entity;

import com.baomidou.mybatisplus.annotation.*;
import lombok.Data;

import java.time.LocalDateTime;
import java.time.LocalDate;
import com.fasterxml.jackson.annotation.JsonFormat;

@Data
@TableName("users")
public class User {
    @TableId(type = IdType.AUTO)
    private Long id;

    @TableField("username")
    private String username;

    @TableField("password")
    private String password;

    @TableField("gender")
    private Gender gender;

    @TableField("phone")
    private String phone;

    @TableField("email")
    private String email;

    @TableField("level")
    private UserLevel level = UserLevel.NORMAL;

    @TableField("points")
    private Integer points = 0;

    @TableField("status")
    private UserStatus status = UserStatus.ACTIVE;

    @TableField("last_login_time")
    private LocalDateTime lastLoginTime;

    @TableField(fill = FieldFill.INSERT)
    private LocalDateTime createTime;

    @TableField(fill = FieldFill.INSERT_UPDATE)
    private LocalDateTime updateTime;

    @TableField("birthday")
    @JsonFormat(pattern = "yyyy-MM-dd")
    private LocalDate birthday;

    public enum Gender {
        MALE("男"),
        FEMALE("女"),
        OTHER("其他");

        private final String displayName;

        Gender(String displayName) {
            this.displayName = displayName;
        }

        public String getDisplayName() {
            return displayName;
        }
    }

    public enum UserLevel {
        NORMAL("普通会员", 0, 999),
        SILVER("银卡会员", 1000, 2999),
        GOLD("金卡会员", 3000, 5999),
        DIAMOND("钻石会员", 6000, Integer.MAX_VALUE);

        private final String displayName;
        private final int minPoints;
        private final int maxPoints;

        UserLevel(String displayName, int minPoints, int maxPoints) {
            this.displayName = displayName;
            this.minPoints = minPoints;
            this.maxPoints = maxPoints;
        }

        public String getDisplayName() {
            return displayName;
        }

        public int getMinPoints() {
            return minPoints;
        }

        public int getMaxPoints() {
            return maxPoints;
        }

        public static UserLevel getLevelByPoints(int points) {
            for (UserLevel level : values()) {
                if (points >= level.minPoints && points <= level.maxPoints) {
                    return level;
                }
            }
            return NORMAL;
        }
    }

    public enum UserStatus {
        ACTIVE("正常"),
        DISABLED("禁用");

        private final String displayName;

        UserStatus(String displayName) {
            this.displayName = displayName;
        }

        public String getDisplayName() {
            return displayName;
        }
    }
}